/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on 29/03/2012, 23:15:30
 */

package server.GUI;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import server.Engine;
import commom.Network;
import server.Slave;
import server.TestSpec;

/**
 *
 * @author gravena
 */
public class Main extends javax.swing.JFrame {
    private ArrayList<Slave> slave = new ArrayList<>();
    private ArrayList<ArrayList<TestSpec>> tests = new ArrayList<>();

    /** Creates new form Main */
    public Main() {
        slave.add(new Slave("127.0.0.1", Network.DEFAULT_PORT, "127.0.0.1" + System.nanoTime(), true));
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelHosts = new javax.swing.JPanel();
        scrollHosts = new javax.swing.JScrollPane();
        tableHosts = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelHosts.setBorder(javax.swing.BorderFactory.createTitledBorder("Connected Slaves"));

        scrollHosts.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                scrollHostsMouseClicked(evt);
            }
        });

        tableHosts.setModel(new tableHostsModel());
        tableHosts.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableHostsMouseClicked(evt);
            }
        });
        scrollHosts.setViewportView(tableHosts);

        javax.swing.GroupLayout panelHostsLayout = new javax.swing.GroupLayout(panelHosts);
        panelHosts.setLayout(panelHostsLayout);
        panelHostsLayout.setHorizontalGroup(
            panelHostsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollHosts, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
        );
        panelHostsLayout.setVerticalGroup(
            panelHostsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollHosts, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelHosts, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelHosts, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void scrollHostsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_scrollHostsMouseClicked
        // TODO add your handling code here:        
    }//GEN-LAST:event_scrollHostsMouseClicked

    private void tableHostsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableHostsMouseClicked
        // TODO add your handling code here:
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new server.GUI.Slave(Main.this.slave.get(Main.this.tableHosts.getSelectedRow())).setVisible(true);

            }
        });
    }//GEN-LAST:event_tableHostsMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    private class tableHostsModel extends AbstractTableModel {

        @Override
        public String getColumnName(int column) {
            if(column == 0) {
                return "IP";
            } else if(column == 1) {
                return "Nome";
            } else if(column == 2) {
                return "Ativo";
            } else {
                return "Não definido.";
            }
        }

        public int getRowCount() {
            return Main.this.slave.size();
        }

        public int getColumnCount() {
            return 3;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if(columnIndex == 0) {
                return Main.this.slave.get(rowIndex).getIp();
            } else if(columnIndex == 1) {
                return Main.this.slave.get(rowIndex).getName();
            } else if(columnIndex == 2) {
                return Main.this.slave.get(rowIndex).isActive();
            } else {
                return null;
            }
        }


    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel panelHosts;
    private javax.swing.JScrollPane scrollHosts;
    private javax.swing.JTable tableHosts;
    // End of variables declaration//GEN-END:variables

}
